]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapOpportunities.swift
Address the lint warnings
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapOpportunities.swift
1 // Copyright (C) 2024 Rubén Beltrán del Río
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
15 import SwiftUI
16
17 struct MapOpportunities: View {
18
19 let mapSize: CGSize
20 let lineWidth: CGFloat
21 let vertexSize: CGSize
22 let opportunities: [Opportunity]
23
24 let arrowheadSize = CGFloat(10.0)
25
26 var body: some View {
27 ForEach(opportunities, id: \.id) { edge in
28 Path { path in
29
30 // First we transform edges from percentage to map coordinates
31 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
32 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
33
34 let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
35 let upperAngle = -CGFloat.pi / 4.0
36 let lowerAngle = CGFloat.pi / 4.0
37
38 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
39 let offsetDestination = CGPoint(
40 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
41
42 path.move(to: offsetOrigin)
43 path.addLine(to: offsetDestination)
44
45 path.move(to: offsetDestination)
46 path.addLine(
47 to: CGPoint(
48 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
49 y:
50 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
51
52 path.move(to: offsetDestination)
53 path.addLine(
54 to: CGPoint(
55 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
56 y:
57 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
58
59 path.move(to: offsetDestination)
60 path.closeSubpath()
61 }.applying(
62 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
63 ).strokedPath(StrokeStyle(lineWidth: lineWidth / 4, dash: [10.0])).stroke(
64 Color.Map.opportunityColor)
65 }
66 }
67
68 func h(_ dimension: CGFloat) -> CGFloat {
69 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
70 }
71
72 func w(_ dimension: CGFloat) -> CGFloat {
73 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
74 }
75 }
76
77 #Preview {
78 MapOpportunities(
79 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
80 vertexSize: CGSize(width: 25.0, height: 25.0),
81 opportunities: [
82 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))
83 ])
84 }